package com.zsc.wxapp.controller;

import com.zsc.wxapp.entity.PythonResponse;
import com.zsc.wxapp.entity.vo.RecognitionVO;
import com.zsc.wxapp.result.Result;
import com.zsc.wxapp.services.UploadImageService;
import com.zsc.wxapp.services.ProcessedDataService;
import com.zsc.wxapp.services.UserService;
import com.zsc.wxapp.utils.MinIOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/upload")
//@Api(tags = "价签识别接口")
@Slf4j
public class UploadImageController {

    @Resource
    private UploadImageService uploadImageService;
    @Resource
    private ProcessedDataService processedDataService;
    @Resource
    private UserService userService;
    @Resource
    private MinIOUtil minIOUtil;

    /**
     * 上传照片并且识别价签
     * @param file
     * @return
     */
    @PostMapping(value = "/local_imagesbed_Upload", produces = "application/json; charset = utf-8")
//    @ApiOperation("静态网页上传照片并且识别价签")
    public Result<RecognitionVO> localImagesBedUpload(@RequestParam(name = "file") MultipartFile file,
                                                      @RequestParam(name = "custUuid") String custUuid,
                                                      @RequestParam(name = "downloadImage") String downloadImage) {
        log.info("开始识别");
//        log.info("对象id是：userId：{}", custUuid);

        // 处理上传的文件
        if (file == null) {
            log.error("文件为空");
            return null;
        }

        if (!downloadImage.equals("true")) {
            downloadImage = "false";
        }

        String url = minIOUtil.uploadFile(file);

        // url改成公网地址
//        url = url.replace("172.16.18.59", "113.106.3.20");

        // 向甲方的服务器发送请求,返回处理之后的json格式的数据
        String json;
        try {
            json = uploadImageService.uploadToExternalAPI(file);
        } catch (Exception e) {
            return Result.error("品规接口调用失败!");
        }

        // 向python端发送请求
        PythonResponse pythonResponse;
        try {
            pythonResponse = uploadImageService.uploadToPython(file, json, url, downloadImage);
        } catch (Exception e) {
            log.error("python调用失败：{}", e);
            String msg = e.getMessage();
            return Result.error(msg);
        }

        if (pythonResponse.getRes().isEmpty() && pythonResponse.getResLoseTag().isEmpty()) {
            return Result.fail("识别失败，请重新上传");
        }

        // 处理python返回的数据
        RecognitionVO recognitionVO = processedDataService.processedDataWithoutPriceRate(pythonResponse, url, downloadImage);

        return Result.success(recognitionVO);
    }

    @PostMapping("/recognition")
//    @ApiOperation("小程序价签识别接口")
    public Result<RecognitionVO> recognition(@RequestParam(name = "file") MultipartFile file,
                                                      @RequestParam(name = "custUuid") String custUuid,
                                                      @RequestParam(name = "downloadImage") String downloadImage) {
        log.info("开始识别");
//        log.info("对象id是：userId：{}", custUuid);

        // 处理上传的文件
        if (file == null) {
            log.error("文件为空");
            return null;
        }

        if (!downloadImage.equals("true")) {
            downloadImage = "false";
        }

//        String url = minIOService.uploadFile(file);
        String url = minIOUtil.uploadFile(file);

        // url改成公网地址
//        url = url.replace("172.16.18.59", "113.106.3.20");

        // 向甲方的服务器发送请求,返回处理之后的json格式的数据
        String json;
        try {
            json = uploadImageService.uploadToExternalAPI(file);
        } catch (Exception e) {
            return Result.error("品规接口调用失败!");
        }

        // 向python端发送请求
        PythonResponse pythonResponse;
        try {
            pythonResponse = uploadImageService.uploadToPython(file, json, url, downloadImage);
        } catch (Exception e) {
            log.error("python调用失败：{}", e);
            String msg = e.getMessage();
            return Result.error(msg);
        }

        if (pythonResponse.getRes().isEmpty() && pythonResponse.getResLoseTag().isEmpty()) {
            return Result.fail("识别失败，请重新上传");
        }

        // 处理python返回的数据
        RecognitionVO recognitionVO = processedDataService.processedData(pythonResponse, url, downloadImage);

        // 添加vip天数
        if (custUuid.equals("null") || custUuid.equals("") || custUuid.isEmpty()) {
            return Result.success(recognitionVO);
        }
        userService.updateVipDays(custUuid);

        return Result.success(recognitionVO);
    }
}


